package com.huaxiafinance.management.controller;

import com.huaxiafinance.management.dto.request.ScoreRuleAddDto;
import com.huaxiafinance.management.dto.request.ScoreRuleDto;
import com.huaxiafinance.management.service.ScoreRuleService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;


@Controller("scoreRuleController")
@RequestMapping("/scoreRule")
public class ScoreRuleController {

  protected Logger logger = Logger.getLogger(ScoreRuleController.class);

  @Autowired
  private ScoreRuleService scoreRuleService;

  /**
   * 请求积分触发条件列表
   *
   * @return 触发条件列表
   */
  @RequestMapping(value = "/queryScoreTriggerList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryScoreTriggerList() {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = scoreRuleService.queryScoreTriggerList();
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("请求积分触发列表异常原因：" + e);
    }
    logger.info("请求积分触发列表返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 请求积分类型列表
   *
   * @return 积分类型列表
   */
  @RequestMapping(value = "/queryScoreTaskList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryScoreTaskList() {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = scoreRuleService.queryScoreTaskList();
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("请求积分类型列表异常原因：" + e);
    }
    logger.info("请求积分类型列表返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 请求触发规则列表
   *
   * @param dto 请求参数
   * @return 触发规则列表
   */
  @RequestMapping(value = "/queryScoreTriggerRules", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryScoreTriggerRules(@Valid @ModelAttribute("dto") ScoreRuleDto dto) throws Exception {
    logger.info("请求触发规则列表的参数:" + dto.toString());
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = scoreRuleService.queryScoreTriggerRules(dto);
    } catch (Exception e) {
      logger.info("请求触发规则列表异常原因:" + e);
      messageResult.setFailureResult();
    }
    logger.info("请求触发规则列表的返回参数:" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 查询积分触发规则详情
   *
   * @param dto 请求参数
   * @return 返回积分规则详情
   */
  @RequestMapping(value = "/queryScoreRuleDetails", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryScoreRuleDetails(@Valid @ModelAttribute("dto") ScoreRuleDto dto) throws Exception {
    logger.info("请求触发规则详情的参数:" + dto.toString());
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = scoreRuleService.queryScoreRuleDetails(dto);
    } catch (Exception e) {
      logger.info("请求触发规则详情的异常原因:" + e);
      messageResult.setFailureResult();
    }
    logger.info("请求触发规则详情的返回参数:" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 检查规则是否已编辑
   *
   * @param dto 请求参数
   * @return 返回检查结果
   */
  @RequestMapping(value = "/inspectRuleName", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String inspectRuleName(@Valid @ModelAttribute("dto") ScoreRuleDto dto) throws Exception {
    logger.info("查询规则名的参数:" + dto.toString());
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = scoreRuleService.inspectRuleName(dto);
    } catch (Exception e) {
      logger.info("查询规则名的异常原因:" + e);
      messageResult.setFailureResult();
    }
    logger.info("查询规则名的返回参数:" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 新增积分触发规则
   *
   * @param dto 请求参数
   * @return 返回新增结果
   */
  @RequestMapping(value = "/insertScoreRule", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String insertScoreRule(@Valid @RequestBody ScoreRuleAddDto dto, BindingResult bindingResult)
      throws Exception {
    logger.info("请求增加触发规则详情的参数:" + dto.toString());
    MessageResult messageResult = new MessageResult();
    try {
      if (bindingResult.hasErrors()) {
        messageResult.setFailureResult(bindingResult);
        return messageResult.getAsJSON();
      }
      messageResult = scoreRuleService.insertScoreRule(dto);
    } catch (Exception e) {
      logger.info("请求增加触发规则详情的异常原因:" + e);
      messageResult.setFailureResult();
    }
    logger.info("请求增加触发规则详情的返回参数:" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 修改规则
   *
   * @param dto 修改规则请求参数
   * @return 修改成功
   */
  @RequestMapping(value = "/updateScoreRule", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateScoreRule(@Valid @RequestBody ScoreRuleAddDto dto, BindingResult bindingResult)
      throws Exception {
    logger.info("请求修改触发规则详情的参数:" + dto.toString());
    MessageResult messageResult = new MessageResult();
    try {
      if (bindingResult.hasErrors()) {
        messageResult.setFailureResult(bindingResult);
        return messageResult.getAsJSON();
      }
      messageResult = scoreRuleService.updateScoreRule(dto);
    } catch (Exception e) {
      logger.info("请求修改触发规则详情的异常原因:" + e);
      messageResult.setFailureResult();
    }
    logger.info("请求修改触发规则详情的返回参数:" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 请求积分等级基础数据
   *
   * @return 积分等级基础数据
   */
  @RequestMapping(value = "/queryScoreGrade", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryScoreGrade() {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = scoreRuleService.queryScoreGrade();
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("请求积分等级异常原因：" + e);
    }
    logger.info("请求积分等级列表返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 删除积分规则
   *
   * @return 积分等级基础数据
   */
  @RequestMapping(value = "/deleteScoreRule", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String deleteScoreRule(@Valid @ModelAttribute("dto") ScoreRuleDto dto) {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = scoreRuleService.deleteScoreRule(dto);
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("请求积分等级异常原因：" + e);
    }
    logger.info("请求积分等级列表返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

}
